package org.example.design.iterator;

/**
 * @author huangshen
 * @version 0
 * @description ArrayCollection
 * @since 2022/2/8 10:51
 */
public class MyArrayContainer<E> implements Container<E> {

    private final Object[] eArr;

    private int last = 0;

    public MyArrayContainer(int maxSize) {
        this.eArr = new Object[maxSize];
    }

    public Object getParamAt(int index) {
        return eArr[index];
    }

    public MyArrayContainer<E> append(E e) {
        this.eArr[last] = e;
        last ++;
        return this;
    }

    public int getLength() {
        return last;
    }

    @Override
    public Iterator<E> iterator() {
        return new MyIterator<>();
    }

    class MyIterator<F> implements Iterator<F> {
        private int index;

        @Override
        public boolean hasNext() {
            return index < MyArrayContainer.this.getLength();
        }

        @Override
        @SuppressWarnings("unchecked")
        public F next() {
            Object object = MyArrayContainer.this.getParamAt(index);
            index ++;
            return (F)object;
        }
    }

}
